<?php

class SJB_Admin_Users_ExportUsers extends SJB_Function
{
	public function isAccessible()
	{
		$this->setPermissionLabel('export_users');
		return parent::isAccessible();
	}

	public function execute()
	{
		ini_set('max_execution_time', 0);
		$tp = SJB_System::getTemplateProcessor();
		$errors = array();

		$exportProperties = SJB_Request::getVar('export_properties', array());
		$userGroupID = SJB_Request::getVar('user_group_id', 0);

		$user = SJB_UsersExportController::createUser($userGroupID);
		$search_form_builder = new SJB_SearchFormBuilder($user);
		$criteria = $search_form_builder->extractCriteriaFromRequestData($_REQUEST, $user);
		$search_form_builder->registerTags($tp);
		$search_form_builder->setCriteria($criteria);

		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			if (empty($exportProperties)) {
				$errors['EMPTY_EXPORT_PROPERTIES'] = true;
			}
			else {
				$inner_join = false;
				if (isset($_REQUEST['product']['multi_like']) && $_REQUEST['product']['multi_like'] != '') {
					$inner_join = array('contracts' => array('join_field' => 'user_sid', 'join_field2' => 'sid', 'join' => 'INNER JOIN'));
				}
				$searcher = new SJB_UserSearcher(false, 'parent_sid', 'ASC', $inner_join);

				$search_aliases = SJB_UsersExportController::getSearchPropertyAliases();
				$found_users_sid = $searcher->getObjectsSIDsByCriteria($criteria, $search_aliases);
				if (!empty($found_users_sid)) {

					$result = true;//SJB_UsersExportController::createExportDirectories();

					if ($result === true) {
						$exportProperties['extUserID'] = 1;
						$exportProperties['parent_sid'] = 1;
						$export_aliases = SJB_UsersExportController::getExportPropertyAliases();
						$export_data = SJB_UsersExportController::getExportData($found_users_sid, $exportProperties, $export_aliases);

						SJB_UsersExportController::changeTreeProperties($export_data);
						SJB_UsersExportController::changeListProperties($export_data);
						SJB_UsersExportController::cleanOptions($export_data);
						SJB_UsersExportController::changeMonetaryProperties($exportProperties, $export_data);
						//SJB_UsersExportController::changeFileProperties($exportProperties, $export_data, 'file');
						//SJB_UsersExportController::changeFileProperties($exportProperties, $export_data, 'video');
						//SJB_UsersExportController::changeFileProperties($exportProperties, $export_data, 'Logo');
						SJB_UsersExportController::changeLocationProperties($exportProperties, $export_data);

						$file_name = 'users.xls';
						
						/******* use csv instead *******/
						$csv = "";
						foreach($exportProperties as $key => $value)
						{
							$csv .= $key.",";
						}
						$csv .= "\r\n";

						foreach ($export_data as $exportItem) {
							foreach($exportItem as $fieldKey => $fieldVal) {
								if(is_array($fieldVal)&&isset($fieldVal))
								{
									$csv .= implode(" ",$fieldVal);
								}else{
									$fieldVal = str_replace(","," ",$fieldVal);
								}
								$csv .= $fieldVal.",";
							} 
							$csv .= "\r\n";
						}
						$csv = str_replace("Array","",$csv);
						header("Content-type:text/csv");   
						header("Content-Disposition:attachment;filename=users.csv");   
						header('Cache-Control:must-revalidate,post-check=0,pre-check=0');   
						header('Expires:0');   
						header('Pragma:public');   
						echo $csv;
						die();
						/**************/
						
						SJB_UsersExportController::makeExportFile($exportProperties, $export_data, $file_name);

						if (!file_exists(SJB_System::getSystemSettings("EXPORT_FILES_DIRECTORY") . "/{$file_name}")) {
							$errors['CANT_CREATE_EXPORT_FILES'] = true;
						}
						else {
							SJB_HelperFunctions::redirect(SJB_System::getSystemSettings("SITE_URL") . "/users/archive-and-send-export-data/");
						}
					}
				}
				else
					$errors['EMPTY_EXPORT_DATA'] = true;
			}
		}
		$userSystemProperties = SJB_UserManager::getAllUserSystemProperties();
		$userGroups = SJB_UserGroupManager::getAllUserGroupsInfo();
		$userCommonProperties = array();
		foreach ($userGroups as $userGroup) {
			$userGroupProperties = SJB_UserProfileFieldManager::getFieldsInfoByUserGroupSID($userGroup['sid']);
			$userCommonProperties[$userGroup['id']] = $userGroupProperties;
		}

		$tp->assign('errors', $errors);
		$tp->assign('userSystemProperties', $userSystemProperties);
		$tp->assign('userCommonProperties', $userCommonProperties);
		$tp->assign('selected_user_group_id', $userGroupID);
		$tp->display('export_users.tpl');
	}
}
